//
//  HHNetworkingManager+Extension.swift
//  HHWeibo
//
//  Created by hehuafeng on 2017/2/21.
//  Copyright © 2017年 雷厉峰行. All rights reserved.
//

import Foundation

extension HHNetworkManager {
    
    /// 新浪微博网络请求封装
    ///
    /// - Parameters:
    ///   - since_id: 若指定此参数,则返回ID比since_id大的微博(即比since_id时间晚的微博),默认为0
    ///   - max_id: 若指定此参数，则返回ID小于或等于max_id的微博，默认为0。
    ///   - completion: 完成时的回调
    func statusList(since_id: Int64 = 0, max_id: Int64 = 0, completion: @escaping (_ list: [[String : Any]], _ isSuccess: Bool) -> ()) {
        // 1. 定义URL
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        // 2. 参数字典
        let param = ["since_id" : "\(since_id)", "max_id" : "\(max_id > 0 ? max_id - 1 : 0)"]
        
        
        // 3. 发送网络请求
        tokenRequest(URLString: urlString, parameters: param) { (json, isSuccess) in
            // 3.1. 获取首页微博数据
            guard let statuses = json as? [String : Any], let result = statuses["statuses"] as? [[String : Any]] else {
                return
            }
            
            // 3.2. 完成回调
            completion(result, isSuccess)
        }
        
    }
    
    /// 获取微博未读消息数
    func unreadCount(completion: @escaping (_ count: Int) -> ()) {
        // 1. 定义URL
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        // 2. 参数字典
        let param = ["uid" : userAccount.uid]
        
        // 3. 发送网络请求
        tokenRequest(URLString: urlString, parameters: param) { (json, isSuccess) in
            // 3.1 获取未读消息数
            guard let result = json as? [String : Any] else {
                return
            }
            let count = result["status"] as? Int
            
            // 3.2 执行回调
            completion(count ?? 0)
        }
    }

}
